package com.android.lovely.ui.dialog

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import com.android.lovely.R
import com.android.lovely.databinding.DialogStarBinding
import com.kotlin.extensions.appContext
import com.kotlin.extensions.view.ViewExt.clickable

class StarDialog(context: Context) : Dialog(context, R.style.Lovely_Dialog), View.OnClickListener {

    private val mBinding by lazy { DialogStarBinding.inflate(layoutInflater, FrameLayout(context), false) }

    var onStar: ((Int) -> Unit)? = null

    private var mRate = 0
        set(value) {
            field = value
            mBinding.star1.isSelected = value >= 1
            mBinding.star2.isSelected = value >= 2
            mBinding.star3.isSelected = value >= 3
            mBinding.star4.isSelected = value >= 4
            mBinding.star5.isSelected = value >= 5
            mBinding.commit.isEnabled = value > 0
        }

    override fun onStart() {
        super.onStart()
        window?.apply {
            setLayout(
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        mBinding.star1.clickable(this)
        mBinding.star2.clickable(this)
        mBinding.star3.clickable(this)
        mBinding.star4.clickable(this)
        mBinding.star5.clickable(this)
        mBinding.commit.clickable(this)
        mBinding.close.clickable(this)
    }

    override fun onClick(v: View?) {
        when (v) {
            mBinding.star1 -> {
                mRate = 1
            }

            mBinding.star2 -> {
                mRate = 2
            }

            mBinding.star3 -> {
                mRate = 3
            }

            mBinding.star4 -> {
                mRate = 4
            }

            mBinding.star5 -> {
                mRate = 5
            }

            mBinding.commit -> {
                if (mRate > 3) {
                    startGooglePlay()
                    onStar?.invoke(mRate)
                    dismiss()
                } else {
                    dismiss()
                }
            }

            mBinding.close -> dismiss()

        }
    }

    private fun startGooglePlay() {
        runCatching {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.data = Uri.parse("market://details?id=${appContext.packageName}")
            intent.setPackage("com.android.vending")
            appContext.startActivity(intent)
        }.onFailure {
            runCatching {
                val browserIntent = Intent().apply {
                    data = Uri.parse("https://play.google.com/store/apps/details?id=${appContext.packageName}")
                }
                context.startActivity(browserIntent)
            }
        }
    }
}